package com.sgb.kjwl.view.ui.fragment.manage;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentEpAssestBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageFragComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageFragModule;
import com.sgb.kjwl.model.entity.manage.EpAssestEntity;
import com.sgb.kjwl.view.ui.adapter.manage.EpAssetAdapter;
import com.sgb.kjwl.viewmodel.manage.EpAssestFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

public class EpAssestFragment extends AppLazyFragment {
    @Inject
    EpAssestFragModel viewModel;

    private FragmentEpAssestBinding binding;

    EpAssetAdapter assestAdapter;
    String typeNo = "";

    Map map;

    public static EpAssestFragment newInstance(String typeNo) {
        EpAssestFragment fragment = new EpAssestFragment();
        Bundle bundle = new Bundle();
        bundle.putString("typeNo", typeNo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerEnterpriseManageFragComponent.builder().appComponent(appComponent)
                .enterpriseManageFragModule(new EnterpriseManageFragModule(this))
                .build()
                .inject(this);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_ep_assest, container, false);
        if (getArguments() != null)
            typeNo = getArguments().getString("typeNo");
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        map = new HashMap();
        assestAdapter = new EpAssetAdapter(getContext(), null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setAdapter(assestAdapter);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                map.put("typeNo", typeNo);
                viewModel.getAssestList(false, map);
            }

            @Override
            public void onLoadMore() {
                map.put("typeNo", typeNo);
                viewModel.getAssestList(true, map);
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    protected void onLazyLoad() {

//        ArrayList<EpAssestEntity> list = new ArrayList<>();
//        EpAssestEntity entity;
//        for (int i = 0; i < 20; i++) {
//            entity = new EpAssestEntity("笔记本电脑", "办公电脑", "联想",
//                    "Gvasda-1123", "￥10000.00", "甘肃蓝野建设监理有限公司新疆分公司", "222");
//            list.add(entity);
//        }
//
//        assestAdapter.setDatas(list);


        map.put("typeNo", typeNo);
        viewModel.getAssestList(false, map);

    }


    public void showList(List<EpAssestEntity> list, boolean isLoadMore) {
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.recyclerView.loadMoreComplete();
                assestAdapter.addDatas(list);
            } else {
                binding.recyclerView.setNoMore(true);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                assestAdapter.setDatas(list);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                assestAdapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }


}
